package com.zzyl.serve.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.CacheConstants;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.mapper.NursingProjectMapper;
import com.zzyl.serve.service.INursingProjectService;
import com.zzyl.serve.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理项目Service业务层处理
 *
 * @author mr.wu
 * @date 2025-05-15
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 查询护理项目
     *
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id) {
        return this.getById(id);
    }

    /**
     * 查询护理项目列表
     *
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject) {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject) {
        return this.save(nursingProject) ? 1 : 0;
    }

    /**
     * 修改护理项目
     *
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject) {
        return this.updateById(nursingProject) ? 1 : 0;
    }

    /**
     * 批量删除护理项目
     *
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids) {
        return this.removeByIds(Arrays.asList(ids)) ? ids.length : 0;
    }

    /**
     * 删除护理项目信息
     *
     * @param id 护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectById(Long id) {
        return this.removeById(id) ? 1 : 0;
    }
//
//    /**
//     * 查询全部护理项目
//     * 优化查询性能redis
//     * 第一种方式，使用List类型
//     *
//     * @return
//     */
//    @Override
//    public List<NursingProjectVo> selectAll() {
//        //先查询redis中是否缓存的有数据
//        List<String> range = redisTemplate.opsForList().range(CacheConstants.NURSING_PROJECT_KEY, 0, -1);
//        List<NursingProjectVo> nursingProjectVo = null;
//        //有数据转换一下，返回
//        if (!CollectionUtils.isEmpty(range)) {
//            nursingProjectVo = range.stream().map(s -> {
//                NursingProjectVo nursingProjectVo1 = JSON.parseObject(s, NursingProjectVo.class);
//                return nursingProjectVo1;
//            }).collect(Collectors.toList());
//        }
//        if (nursingProjectVo == null || CollUtil.isEmpty(nursingProjectVo)){
//            List<NursingProject> nursingProjectList = this.lambdaQuery().eq(NursingProject::getStatus, 1)
//                    .select(NursingProject::getName, NursingProject::getId)
//                    .list();
//            nursingProjectVo = nursingProjectList.stream().map(p -> {
//                return NursingProjectVo.builder().label(p.getName()).value(p.getId().toString()).build();
//            }).collect(Collectors.toList());
//            //将数据缓存到redis中
//            redisTemplate.opsForList().leftPush(CacheConstants.NURSING_PROJECT_KEY, JSON.toJSONString(nursingProjectVo));
//        }
//        return nursingProjectVo;
//    }

//
//    /**
//     * 查询全部护理项目
//     * 优化查询性能redis
//     * 第二种方式，使用String类型
//     *
//     * @return
//     */
    @Override
    public List<NursingProjectVo> selectAll() {
        //先查询redis中是否缓存的有数据
        String range = redisTemplate.opsForValue().get(CacheConstants.NURSING_PROJECT_KEY);
        List<NursingProjectVo> nursingProjectVo = null;
        //有数据转换一下，返回
        if (!StrUtil.isBlank(range)) {
             nursingProjectVo = JSON.parseArray(range, NursingProjectVo.class);
        }
        if (nursingProjectVo == null || CollUtil.isEmpty(nursingProjectVo)){
            List<NursingProject> nursingProjectList = this.lambdaQuery().eq(NursingProject::getStatus, 1)
                    .select(NursingProject::getName, NursingProject::getId)
                    .list();
            nursingProjectVo = nursingProjectList.stream().map(p -> {
                return NursingProjectVo.builder().label(p.getName()).value(p.getId().toString()).build();
            }).collect(Collectors.toList());
            //将数据缓存到redis中
            redisTemplate.opsForValue().set(CacheConstants.NURSING_PROJECT_KEY, JSON.toJSONString(nursingProjectVo));
        }
        return nursingProjectVo;
    }
}
